package com.creative.accountsdk.holder;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import com.creative.accountsdk.AccountActivity;
import com.creative.accountsdk.AccountAgreementActivity;
import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.AccountUtils;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.BindProtocol;
import com.creative.accountsdk.net.account.MergeProtocol;
import com.creative.accountsdk.net.account.RegisteProtocol;
import com.creative.accountsdk.net.account.ThirdMergeProtocol;

import cz.msebera.android.httpclient.Header;
import org.json.JSONObject;

/**
 * Created by yinjialiang on 15-6-24.
 */
public class BindWawayayaHolder extends BaseHolder{
    private EditText editTextUsername, editTextPassword;
    private Dialog progress;
    private String username, password;

    private final static String TAG = BindWawayayaHolder.class.getSimpleName();

    @Override
    protected void initViews() {
        editTextUsername = (EditText) viewRoot.findViewById(R.id.editTextUsername);
        editTextPassword = (EditText) viewRoot.findViewById(R.id.editTextPassword);

        viewRoot.findViewById(R.id.textViewAgreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, AccountAgreementActivity.class);
                activity.startActivity(intent);
            }
        });

        viewRoot.findViewById(R.id.buttonOK).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bind();
            }
        });
    }

    private void bind()
    {
        username = editTextUsername.getText().toString().trim();
        password = editTextPassword.getText().toString().trim();
        if (username.equals("")) {
            Toast.makeText(activity, R.string.account_username_empty, Toast.LENGTH_SHORT).show();
        }
        else if(!AccountUtils.isValidEmail(username))
        {
            Toast.makeText(activity, R.string.account_invalid_email, Toast.LENGTH_SHORT).show();
        }
        else if (password.equals("")) {
            Toast.makeText(activity, R.string.account_password_empty, Toast.LENGTH_SHORT).show();
        } else if (username.length() < 6) {
            Toast.makeText(activity, R.string.account_username_length, Toast.LENGTH_SHORT).show();
        } else if (password.length() < 6) {
            Toast.makeText(activity, R.string.account_password_length, Toast.LENGTH_SHORT).show();
        } else {
            progress = DialogUtils.createProgress(activity);
            progress.show();
            final BindProtocol protocol = new BindProtocol(username, password);
            protocol.debugUrl(DataCenter.getHttpClient());
            DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {

                @Override
                public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                    super.onSuccess(statusCode, headers, response);
                    progress.cancel();
                    if (protocol.parse(response, activity)) {
                        if(protocol.existAccount)
                        {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setMessage(R.string.account_merge)
                                    .setPositiveButton(R.string.account_button_agree, new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            merge();
                                        }
                                    })
                                    .setNegativeButton(R.string.account_button_cancel, null);
                            builder.create().show();
                        }
                        else {
                            UserData.getInstance().init(protocol.jsonUser);
                            UserData.getInstance().write(activity);
                            activity.finish();
                            OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                            if (loginListener != null) {
                                loginListener.onComplete(protocol.jsonUser);
                            }
                        }
                    }
                }
            });
        }
    }

    private void merge()
    {
        progress = DialogUtils.createProgress(activity);
        progress.show();
        final MergeProtocol protocol = new MergeProtocol(username, password);
        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().get(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress){

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if(protocol.parse(response, activity))
                {
                    Log.d(TAG, "merge success");
                    UserData.getInstance().init(protocol.jsonUser);
                    UserData.getInstance().write(activity);
                    activity.finish();
                    OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                    if (loginListener != null) {
                        loginListener.onComplete(protocol.jsonUser);
                    }
                }
            }
        });
    }

    @Override
    public int getLayoutResID() {
        return R.layout.account_bind_wawayaya;
    }
}
